<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Ubutu下编译ijk player教程 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Ubutu下编译ijk player教程</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">December 22, 2017&nbsp;&nbsp;22:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Linux/">Linux</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h3 id="关于ijkplayer："><a href="#关于ijkplayer：" class="headerlink" title="关于ijkplayer："></a>关于ijkplayer：</h3><p>基于 FFmpeg n3.1 的 Android/iOS 视频播放器，支持 MediaCodec, VideoToolbox，支持 RTMP/HTTP/HTTPS 视频流播放，支持本地视频播放。<br>项目地址：<a href="https://github.com/Bilibili/ijkplayer" target="_blank" rel="noopener">https://github.com/Bilibili/ijkplayer</a><br>编译环境：linux或mac</p>
<h3 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h3><h4 id="1-所需环境"><a href="#1-所需环境" class="headerlink" title="1) 所需环境"></a>1) 所需环境</h4><ul>
<li>系统：<a href="https://www.ubuntu.com/download/desktop" target="_blank" rel="noopener">Ubuntu 16.04 </a>(并没有采用虚拟机，而是在真实系统下编译的，无论是虚拟机还是真实系统需要保证25G以上的硬盘空间，否则到后期存储空间不足很麻烦)</li>
<li>NDK：<a href="https://developer.android.google.cn/ndk/downloads/index.html" target="_blank" rel="noopener">android-ndk-r14b</a>（需要使用 r10e 以上版本）</li>
<li>SDK：<a href="https://developer.android.google.cn/studio/index.html" target="_blank" rel="noopener">android-sdk</a>（我采用了下载studio的方式获取SDK，会导致下载包过大，但是操作简单，能够保证SDK功能完整）</li>
</ul>
<h4 id="2-打开终端，执行下面命令，安装git、vim、yasm"><a href="#2-打开终端，执行下面命令，安装git、vim、yasm" class="headerlink" title="2) 打开终端，执行下面命令，安装git、vim、yasm"></a>2) 打开终端，执行下面命令，安装git、vim、yasm</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install git</span><br><span class="line">sudo apt-get install yasm</span><br><span class="line">sudo apt-get install vim</span><br></pre></td></tr></table></figure>

<h4 id="3-克隆ijkplayer代码"><a href="#3-克隆ijkplayer代码" class="headerlink" title="3) 克隆ijkplayer代码"></a>3) 克隆ijkplayer代码</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 把代码<span class="built_in">clone</span>到本地</span><br><span class="line">git <span class="built_in">clone</span> https://github.com/Bilibili/ijkplayer ijkplayer-android</span><br></pre></td></tr></table></figure>

<h4 id="4-配置环境变量"><a href="#4-配置环境变量" class="headerlink" title="4) 配置环境变量"></a>4) 配置环境变量</h4><p>在开始编译之前需要先配置ANDROID_SDK与ANDROID_NDK环境变量。<br>下载（步骤1处得到）解压SDK和NDK，获取到保存路径。<br>在命令行终端中运行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo vim /etc/profile</span><br></pre></td></tr></table></figure>
<p>在文件末尾添加如下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> ANDROID_NDK=/opt/Android/android-ndk-r13b</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$ANDROID_NDK</span></span><br><span class="line"><span class="built_in">export</span> ANDROID_SDK=/opt/Android/sdk</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$ANDROID_SDK</span>/tools:<span class="variable">$ANDROID_SDK</span>/platform-tools</span><br></pre></td></tr></table></figure>
<p>然后执行：<br>source /etc/profile 或重启后生效。</p>
<h3 id="编译ijkplayer"><a href="#编译ijkplayer" class="headerlink" title="编译ijkplayer"></a>编译ijkplayer</h3><h4 id="编译之前的配置"><a href="#编译之前的配置" class="headerlink" title="编译之前的配置"></a>编译之前的配置</h4><p>下面三种是ijkplayer给出的配置，我这里执行的是第一种（支持的格式比较全，所以为了避免格式不支持问题，建议使用第一种。但是打出的SO体积也是最大在10M左右，打成apk包压缩后大约在5M）</p>
<p>If you prefer more codec/format</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> config</span><br><span class="line">rm module.sh</span><br><span class="line">ln -s module-default.sh module.sh</span><br><span class="line"><span class="built_in">cd</span> ..</span><br><span class="line"><span class="built_in">cd</span> android/contrib</span><br><span class="line"><span class="comment"># cd ios</span></span><br><span class="line">sh compile-ffmpeg.sh clean</span><br></pre></td></tr></table></figure>

<p>If you prefer less codec/format for smaller binary size (include hevc function)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> config</span><br><span class="line">rm module.sh</span><br><span class="line">ln -s module-lite-hevc.sh module.sh</span><br><span class="line"><span class="built_in">cd</span> android/contrib</span><br><span class="line"><span class="comment"># cd ios</span></span><br><span class="line">sh compile-ffmpeg.sh clean</span><br></pre></td></tr></table></figure>

<p>If you prefer less codec/format for smaller binary size (by default)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> config</span><br><span class="line">rm module.sh</span><br><span class="line">ln -s module-lite.sh module.sh</span><br><span class="line"><span class="built_in">cd</span> android/contrib</span><br><span class="line"><span class="comment"># cd ios</span></span><br><span class="line">sh compile-ffmpeg.sh clean</span><br></pre></td></tr></table></figure>

<h4 id="1-下载ijkplayer所需的FFmpeg代码"><a href="#1-下载ijkplayer所需的FFmpeg代码" class="headerlink" title="1) 下载ijkplayer所需的FFmpeg代码"></a>1) 下载ijkplayer所需的FFmpeg代码</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 进入工程根目录</span><br><span class="line"><span class="built_in">cd</span> ijkplayer-android</span><br><span class="line">// checkout ffmpeg 源码，默认从 https://github.com/Bilibili/FFmpeg.git checkout</span><br><span class="line">./init-android.sh</span><br></pre></td></tr></table></figure>

<h4 id="2-下载ijkplayer对https协议支持的代码"><a href="#2-下载ijkplayer对https协议支持的代码" class="headerlink" title="2) 下载ijkplayer对https协议支持的代码"></a>2) 下载ijkplayer对https协议支持的代码</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// checkout openssl android源码，默认从 https://github.com/Bilibili/openssl.git checkout</span><br><span class="line">./init-android-openssl.sh</span><br></pre></td></tr></table></figure>

<h3 id="开始编译"><a href="#开始编译" class="headerlink" title="开始编译"></a>开始编译</h3><h4 id="1-编译-openssl"><a href="#1-编译-openssl" class="headerlink" title="1) 编译 openssl"></a>1) 编译 openssl</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 首先 <span class="built_in">cd</span> 到 ijkplayer/android/contrib 目录下，Android下编译脚本就放在该目录下</span><br><span class="line"><span class="built_in">cd</span> android/contrib</span><br><span class="line">// 编译 openssl</span><br><span class="line">./compile-openssl.sh clean</span><br><span class="line">./compile-openssl.sh all</span><br></pre></td></tr></table></figure>

<h4 id="2-编译-ffmpeg"><a href="#2-编译-ffmpeg" class="headerlink" title="2) 编译 ffmpeg"></a>2) 编译 ffmpeg</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">./compile-ffmpeg.sh clean</span><br><span class="line">./compile-ffmpeg.sh all</span><br></pre></td></tr></table></figure>
<p>此过程时间较长并且会出现如下警告，不用做任何处理，耐心等待即可：</p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/Ubutu%E4%B8%8B%E7%BC%96%E8%AF%91ijk%20player%E6%95%99%E7%A8%8B/1.png?raw=true" alt=""></p>
<h4 id="3-编译ijkplayer生成CPU架构的so"><a href="#3-编译ijkplayer生成CPU架构的so" class="headerlink" title="3) 编译ijkplayer生成CPU架构的so"></a>3) 编译ijkplayer生成CPU架构的so</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 返回到 ijkplayer-android/android 目录</span><br><span class="line"><span class="built_in">cd</span> ..</span><br><span class="line">// 编译生成各CPU架构的so，如果不加 all 默认只生成 armv7a 架构的 so</span><br><span class="line">// 编译指定架构的so：compile-ijk.sh armv7a，具体参考 .sh 文件中的 Usage</span><br><span class="line">./compile-ijk.sh all</span><br></pre></td></tr></table></figure>

<p>编译完成后 so 就放在 ijkplayer/android/ijkplayer/ijkplayer-xxx/src/main/libs 目录下。此时编译好的 so 是支持 https 协议的。<br>ijkplayer-android/android/ijkplayer 是一个 Android Studio 工程，里面有 ijkplayer 的 example 和各架构的 Library Project。<br>ijkplayer/android/ijkplayer 是一个 Android Studio 工程，里面有 ijkplayer 的 example 和各架构的 Library Project。<br>此时，把 ijkplayer-android/android/ijkplayer 导入 Android Studio ，ijkplayer-example 已经可以正常运行了。</p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/Ubutu%E4%B8%8B%E7%BC%96%E8%AF%91ijk%20player%E6%95%99%E7%A8%8B/2.png?raw=true" alt=""> </p>
<h4 id="4-在自己的工程中引用-ijkplayer-和自己编译的-so"><a href="#4-在自己的工程中引用-ijkplayer-和自己编译的-so" class="headerlink" title="4) 在自己的工程中引用 ijkplayer 和自己编译的 so"></a>4) 在自己的工程中引用 ijkplayer 和自己编译的 so</h4><p>(1) 在 build.gradle 添加 ijkplayer-java 依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">compile <span class="string">'tv.danmaku.ijk.media:ijkplayer-java:0.8.4'</span></span><br><span class="line">// compile <span class="string">'tv.danmaku.ijk.media:ijkplayer-exo:0.8.4'</span> // 这个是 ijkplayer 对 exoplayer 的封装，支持webm格式视频，看项目需求是否添加</span><br></pre></td></tr></table></figure>
<p>(2) 在工程 src\main 目录下新建一个 jniLibs 目录并把 so 拷贝到对应架构名称的目录下。<br>如果不想引入所有的so文件，可以在Module：app中加入以下代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">android &#123;</span><br><span class="line">    defaultConfig &#123;</span><br><span class="line">        ndk &#123;</span><br><span class="line">            abiFilter <span class="string">"armeabi-v7a"</span>  <span class="comment">//, 'x86', 'armeabi', 'x86_64', 'arm64-v8a'</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2017/12/22/Ubutu%E4%B8%8B%E7%BC%96%E8%AF%91ijk%20player%E6%95%99%E7%A8%8B/">https://zhangpingv5.gitee.io/2017/12/22/Ubutu%E4%B8%8B%E7%BC%96%E8%AF%91ijk%20player%E6%95%99%E7%A8%8B/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/Linux/"># Linux</a>
                    
                        <a href="/tags/ijk-player/"># ijk player</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2017/12/22/Linux%E4%B8%8B%E8%AE%BE%E7%BD%AE%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/">Linux下设置环境变量</a>
            
            
            <a class="next" rel="next" href="/2017/12/12/JavaScript--es6/">JavaScript--es6</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
